package com.yunda.nebula.gateway.filter;

import com.yunda.nebula.gateway.service.RequestMatcher;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Service;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.util.Date;

/**
 * \* User: wei.zhang
 * \* Date: 2019/11/7 20:33
 * \* Description:
 * \
 */
@Slf4j
@Service
public class LogGatewayFilter implements GlobalFilter, Ordered {

    public static final int LOAD_LOG_ORDER = 10000;

    @Autowired
    private RequestMatcher requestMatcher;

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        //获取header的参数
        ServerHttpRequest serverHttpRequest = exchange.getRequest();
        String path = serverHttpRequest.getURI().getPath();
        String token = serverHttpRequest.getHeaders().getFirst("token");
        String date = DateFormatUtils.format(new Date(),"yyyy-mm-dd HH:mm:ss");
        String userAgent = serverHttpRequest.getHeaders().getFirst("User-Agent");
        log.info("访问地址为 {} 访问时间为 {} 用户token 为 {}" ,path,date,token);
        return chain.filter(exchange);
    }


    @Override
    public int getOrder() {
        return LOAD_LOG_ORDER;
    }
}
